package com.junxonline.instant.admin.controller.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junxonline.instant.dao.entity.model.config.User;
import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.UserVO;
import com.junxonline.instant.admin.service.config.OrganizationService;
import com.junxonline.instant.admin.service.config.RoleService;
import com.junxonline.instant.admin.service.config.UserService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户管理Controller
 *
 * @author JunX
 * @date 2021-06-24
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询组织机构树（TreeSelect）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:query")
    @OperationLog(label = "用户管理-查询组织机构树", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryOrganizationTree")
    public CommonResult queryOrganizationTree() {
        return CommonResult.success(organizationService.queryOrganizationTree(new QueryWrapper<>()));
    }

    /**
     * 查询角色列表（Select）
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:query")
    @OperationLog(label = "用户管理-查询角色列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryRoleList")
    public CommonResult queryRoleList() {
        return CommonResult.success(roleService.queryRoleList(new QueryWrapper<>()));
    }

    /**
     * 查询用户列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:query")
    @OperationLog(label = "用户管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<UserVO> vo) {
        return CommonResult.success(userService.queryList(vo, new QueryWrapper<>()));
    }

    /**
     * 查询用户
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:query")
    @OperationLog(label = "用户管理-查询用户", type = OperationEnum.QUERY)
    @PostMapping(value = "/query")
    public CommonResult query(@RequestBody(required = false) UserVO vo) {
        if (vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(userService.query(vo.getId()));
    }

    /**
     * 新增用户
     *
     * @param user 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:add")
    @OperationLog(label = "用户管理-新增用户", type = OperationEnum.ADD)
    @PostMapping(value = "/add")
    public CommonResult add(@RequestBody(required = false) User user) {
        if (user == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        userService.add(user);
        return CommonResult.success();
    }

    /**
     * 编辑用户
     *
     * @param user 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:edit")
    @OperationLog(label = "用户管理-编辑用户", type = OperationEnum.EDIT)
    @PostMapping(value = "/edit")
    public CommonResult edit(@RequestBody(required = false) User user) {
        if (user == null || user.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        if (user.getId() == 1 && user.getStatus() == 0) {
            // TODO 处理超级管理员特殊逻辑
            return CommonResult.fail(AdminErrorEnum.USER_ADMIN_NOT_DISABLE);
        }
        userService.edit(user);
        return CommonResult.success();
    }

    /**
     * 删除用户
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:delete")
    @OperationLog(label = "用户管理-删除用户", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        for (Long id : idsVO.getIds()) {
            if (id == 1) {
                // TODO 处理超级管理员特殊逻辑
                return CommonResult.fail(AdminErrorEnum.USER_ADMIN_NOT_DELETE);
            }
        }
        userService.delete(idsVO.getIds());
        return CommonResult.success();
    }

    /**
     * 重置用户密码
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:user:edit")
    @OperationLog(label = "用户管理-重置用户密码", type = OperationEnum.EDIT)
    @PostMapping(value = "/resetPassword")
    public CommonResult resetPassword(@RequestBody(required = false) UserVO vo) {
        if (vo == null || vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        if (vo.getId() == 1) {
            // TODO 处理超级管理员特殊逻辑
            return CommonResult.fail(AdminErrorEnum.USER_ADMIN_NOT_RESET_PASSWORD);
        }
        userService.resetPassword(vo.getId());
        return CommonResult.success("密码重置成功请尽快修改！");
    }

}
